package service.impl;

import bean.CutPageBean;
import bean.GoodsBean;
import mapper.IGoodsMapper;
import org.apache.ibatis.session.SqlSession;
import service.IGoodsService;

import java.time.LocalDate;
import java.util.List;

public class GoodsServiceImpl extends BaseService implements IGoodsService {
    @Override
    public int add(GoodsBean goodsBean) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.add(goodsBean);
        session.commit();
        session.close();
        return goodsBean.getId();
    }

    @Override
    public void del(Integer id) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer id, Integer price) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.update(id,price);
        session.commit();
        session.close();
    }

    @Override
    public GoodsBean findById(Integer id) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        GoodsBean goodsBean = mapper.findById(id);
        session.close();
        return goodsBean;
    }

    @Override
    public CutPageBean<GoodsBean> cutByItem(int pageNO, String name, Integer startPrice, Integer endPrice) {
        CutPageBean cutPageBean = new CutPageBean();
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        cutPageBean.setList(mapper.cutByItemList((pageNO - 1) * CutPageBean.PAGE_SIZE, CutPageBean.PAGE_SIZE, name, startPrice, endPrice));
        cutPageBean.setCount(mapper.cutByItemCount(name, startPrice, endPrice));
        session.close();
        return cutPageBean;
    }

    @Override
    public CutPageBean<GoodsBean> cutByFactory(int pageNO, String factory, LocalDate startDate, LocalDate endDate) {
        CutPageBean cutPageBean = new CutPageBean();
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        cutPageBean.setList(mapper.cutByFactoryList((pageNO - 1) * CutPageBean.PAGE_SIZE, CutPageBean.PAGE_SIZE, factory, startDate, endDate));
        cutPageBean.setCount(mapper.cutByFactoryCount(factory, startDate, endDate));
        session.close();
        return cutPageBean;
    }

    @Override
    public void addList(List<GoodsBean> list) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.addList(list);
        session.commit();
        session.close();
    }

    @Override
    public void delList(int[] idArray) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.delList(idArray);
        session.commit();
        session.close();
    }
}
